package com.link2room.rodimus.facade.impl;

import cn.brandwisdom.gss.param.GssCreateParamDto;
import cn.brandwisdom.gss.param.GssResponseParamDto;
import cn.brandwisdom.gss.result.ResutlGssDto;
import com.link2room.blaster.constant.MsgConstant;
import com.link2room.blaster.util.MessageFlatPad;
import com.link2room.common.service.ICommonService;
import com.link2room.common.util.MD5Util;

import com.link2room.rodimus.constant.MemberConstant;
import com.link2room.rodimus.entity.CommentBrandwisdomQuestionnaire;
import com.link2room.rodimus.entity.CommentBrandwisdomSend;
import com.link2room.rodimus.entity.MemberBase;
import com.link2room.rodimus.facade.IAfterPMSdepDoFacadeService;
import com.link2room.rodimus.facade.ICommentFacadeService;
import com.link2room.rodimus.feign.soundwave.PmsMemberFeignService;
import com.link2room.rodimus.service.IGSSService;
import com.link2room.soundwave.dto.param.inside.PmsReservationInsideParamDto;
import com.link2room.soundwave.dto.param.inside.PointPresentUseInsideParamDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by VegetaKo on 2017/2/28.
 */
@Component("commentFacadeService")
public class CommentFacadeServiceImpl implements ICommentFacadeService,IAfterPMSdepDoFacadeService {
    @Resource
    private ICommonService commonService;

    @Resource(name="gSSService")
    private IGSSService gSSService;

    @Resource
    private MessageFlatPad messageFlatPad;

    @Autowired
    private PmsMemberFeignService pmsMemberFeignService;

    @Override
    public void newBarndwisdom(PmsReservationInsideParamDto pmsReservationInsideParamDto) throws Exception {
        MemberBase memberBaseFinder = new MemberBase();
        memberBaseFinder.setOpenid( pmsReservationInsideParamDto.getOpenid());
        memberBaseFinder.setLhotelGroupCode( pmsReservationInsideParamDto.getLhotelGroupCode());
        MemberBase memberBase = commonService.findOneEQ( MemberBase.class, memberBaseFinder);
        if( null!=memberBase&& MemberConstant.IS_PMSMEMBER.TRUE.equals(memberBase.getIsbindPms()))
        {
            CommentBrandwisdomQuestionnaire cbqFinder = new CommentBrandwisdomQuestionnaire();
            cbqFinder.setLhotelGroupCode( pmsReservationInsideParamDto.getLhotelGroupCode());
            cbqFinder.setLhotelCode(pmsReservationInsideParamDto.getLhotelCode());
            CommentBrandwisdomQuestionnaire cbq ;
            cbq=  commonService.findOneEQ(CommentBrandwisdomQuestionnaire.class, cbqFinder);
            if(cbq!=null){
                String secret="c8c6c05aacb8ddbc82dee8a53f63667d";
                GssCreateParamDto gssCreateParamDto =new GssCreateParamDto();
                gssCreateParamDto.setApp_key("nyinn");
                //gssCreateParamDto.setIssue_id(cbq.getIssueid());
                gssCreateParamDto.setIssue_id(cbq.getIssueid());
                gssCreateParamDto.setMember_num(pmsReservationInsideParamDto.getMemberNo());
                gssCreateParamDto.setMethod("gss.set_pms_user");
                gssCreateParamDto.setPms_api("http://notify.link2room.com/out/rodimus/third/Brandwisdom/GssCallback");
                gssCreateParamDto.setPms_code( "1010000910");
                gssCreateParamDto.setTimestamp(Long.valueOf(System.currentTimeMillis()/1000).toString());
                gssCreateParamDto.setUser_name( pmsReservationInsideParamDto.getRsvMan());
                String preSign=secret+"app_key"+gssCreateParamDto.getApp_key()+
                        "issue_id"+gssCreateParamDto.getIssue_id()+
                        "member_num"+gssCreateParamDto.getMember_num()+
                        "method"+gssCreateParamDto.getMethod()+
                        "pms_api"+gssCreateParamDto.getPms_api()+
                        "pms_code"+gssCreateParamDto.getPms_code()+
                        "timestamp"+gssCreateParamDto.getTimestamp()+
                        "user_name"+gssCreateParamDto.getUser_name();
                MD5Util.getMd5_32(preSign );
                //String Sign="959e2d4e24a210d90a27b7092cfb14d7";
                gssCreateParamDto.setSign(MD5Util.getMd5_32(preSign ));
                gssCreateParamDto.setLhotelGroupCode(null);
                gssCreateParamDto.setOptId(null);
                gssCreateParamDto.setLanguage(null);
                ResutlGssDto  resutlGssDto =gSSService.getBrandwisdomGss( gssCreateParamDto );

                CommentBrandwisdomSend commentBrandwisdomSend =new CommentBrandwisdomSend();
                commentBrandwisdomSend.setLhotelGroupCode(pmsReservationInsideParamDto.getLhotelGroupCode() );
                commentBrandwisdomSend.setL2RNo(pmsReservationInsideParamDto.getL2RNo() );
                commentBrandwisdomSend.setMemberNo( pmsReservationInsideParamDto.getMemberNo());
                commentBrandwisdomSend.setIssueid( resutlGssDto.getData().getIssueid());
                commentBrandwisdomSend.setCallid(resutlGssDto.getData().getCallid() );
                commentBrandwisdomSend.setSendSta("INIT");
                commentBrandwisdomSend.setReciveSta("INIT");
                commonService.save(commentBrandwisdomSend );

                Map news = new HashMap();
                news.put("url",resutlGssDto.getData().getAnsurl());
                news.put("openid",pmsReservationInsideParamDto.getOpenid());
                news.put("description","答题赠送积分请点击详情");
                news.put("title","客户满意度调查问卷");
                messageFlatPad.send(pmsReservationInsideParamDto.getLhotelGroupCode(), MsgConstant.CHANNEL.WECHA_CUSTOM_SEND,MsgConstant.TEMPLET_CODE.BRANDWISDOM_GSS,news);
            }
        }

    }

    @Override
    public void awarBarndwisdom(GssResponseParamDto gssResponseParamDto) throws Exception {
        CommentBrandwisdomSend commentBrandwisdomSendFinder =new CommentBrandwisdomSend();
        commentBrandwisdomSendFinder.setCallid( gssResponseParamDto.getCallid());
        commentBrandwisdomSendFinder.setIssueid( gssResponseParamDto.getIssueid());
        CommentBrandwisdomSend commentBrandwisdomSend= commonService.findOneEQ(CommentBrandwisdomSend.class ,commentBrandwisdomSendFinder);
        if(null==commentBrandwisdomSend|| !commentBrandwisdomSend.getReciveSta().equals("INIT") ){
            return;
        }
        else{

            PointPresentUseInsideParamDto pointPresentUseInsideParamDto = new PointPresentUseInsideParamDto();
            pointPresentUseInsideParamDto.setLhotelGroupCode( commentBrandwisdomSend.getLhotelGroupCode());
            pointPresentUseInsideParamDto.setL2rMemberNo( commentBrandwisdomSend.getMemberNo());
            pointPresentUseInsideParamDto.setPoint(new BigDecimal("200"));
            pointPresentUseInsideParamDto.setRemark("荟评点评赠送");
            pointPresentUseInsideParamDto.setPromotionTag("");
            pmsMemberFeignService.addPoint( pointPresentUseInsideParamDto);

            commentBrandwisdomSend.setReciveSta( "AWARD");
            commonService.merge(commentBrandwisdomSend);
        }
    }

    @Override
    public void afterPMSDepDo(PmsReservationInsideParamDto pmsReservationInsideParamDto) throws Exception {
        this.newBarndwisdom(pmsReservationInsideParamDto);
    }
}
